package com.cfx.exercise_39;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class PDFClass {

	public static void main(String[] args) throws DocumentException, IOException {
		Random ran = new Random();
		Document doc = new Document();
		//设置页面
		Rectangle rec = new Rectangle(PageSize.A4);
		doc.setPageSize(rec);
		doc.setMargins(20, 20, 40, 40);
		//设置字体
		BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font chineseFont = new Font(baseFont,15,Font.NORMAL);
		Font font1 = new Font(baseFont,10,Font.BOLD);
		Font font2 = new Font(baseFont,20,Font.COURIER);
		Font font3 = new Font(baseFont,12,Font.NORMAL);
		try {
			PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream("pdf/example"+ran.nextInt(100)+".pdf"));
			
			//文档属性
			doc.addTitle("Title@sample"); // 标题 
			doc.addAuthor("Author@zhangsan");// 作者 
			doc.addSubject("Subject@iText sample");// 主题 
			doc.addKeywords("Keywords@iText");// 关键字 
			doc.addCreator("Creator@iText");// 创建者 
			
			doc.open();
			
			Paragraph title = new Paragraph("空调设备采购及安装合同",font2);
			title.setAlignment(Element.ALIGN_CENTER);//设置居中
			title.setLeading(10f);//设置与其上方的距离

			Paragraph number = new Paragraph("合同编号:WHD 150913\n日\b期:2015-09-13",font1);
			number.setIndentationLeft(400f);//设置左距
			Paragraph head = new Paragraph("甲方：    XXXX\n乙方：深圳市万浩达空调技术工程有限公司",chineseFont);
			Paragraph text = new Paragraph("甲方因工程建设需要，向乙方购买以下空调设备，甲乙双方本着平等、互利、协商、一致的原 则，就产品买卖及安装事宜，签订本合同书如下：\n一、产品名称：大金中央空调。\n二、项目总价：RMB 63000.00  元 (人民币 陆万叁仟 元整) （未含税）",font3);
			//text.setLeading(4f);
			PdfPTable table = new PdfPTable(6);
			table.setWidthPercentage(50);
			table.setSpacingBefore(20f);
			//设置行宽
			float[] colWidths = {2.5f,5f,1.25f,1.25f,2f,2f};
			table.setWidths(colWidths);
			
			PdfPCell cell1 = new PdfPCell(new Paragraph("名称",font3));			
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell2 = new PdfPCell(new Paragraph("型号",font3));
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell3 = new PdfPCell(new Paragraph("单位",font3));
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell4 = new PdfPCell(new Paragraph("数量",font3));
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell5 = new PdfPCell(new Paragraph("单价",font3));
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell6 = new PdfPCell(new Paragraph("合计",font3));
            cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
	        table.addCell(cell1);
	        table.addCell(cell2);
	        table.addCell(cell3);
	        table.addCell(cell4);
	        table.addCell(cell5);
	        table.addCell(cell6);
			text.setFirstLineIndent(20f);
			doc.add(title);
			doc.add(new Paragraph("\n"));
			doc.add(number);
			doc.add(new Paragraph("\n"));
			doc.add(head);
			doc.add(text);
			doc.add(table);
			doc.close();
			pw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
